import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        ArrayList<Page> list = new ArrayList<Page>();
        Page page1 = new Page("www.baidu.com",0,0);
        Page page2 = new Page("www.douban.com",0,0);
        Page page3 = new Page("www.zhihu.com",0,0);
        list.add(page1);
        list.add(page2);
        list.add(page3);

        Date time = new Date();
        Scanner scanner = new Scanner(System.in);

        while(true){
            System.out.println("请输入序号选择以下功能:\n1.访问页面\n2.显示访问量排行榜\n" +
                    "3.查看总访问量\n4.查看平均访问量\n5.显示页面最后访问时间\n6.清空访问统计\n7.退出系统");

            String choice =  scanner.nextLine();


            switch (choice){
                case "1":
                    System.out.println("请输入序号选择访问以下网站：1.百度 2.豆瓣 3.知乎");
                    String input = scanner.nextLine();
                    switch (input){
                        case "1": visitPage(page1,time); break;
                        case "2": visitPage(page2,time); break;
                        case "3": visitPage(page3,time); break;
                        default:
                            System.out.println("指令错误"); break;

                    }
                    break;
                case "2":
                    showVisitRank(list);
                    break;
                case "3":
                    totalVisit(list);
                    break;
                case "4":
                    averageVisit(list);
                    break;
                case "5":
                    lastVisitTime(list);
                    break;
                case "6":
                    clearStatistics(list);
                    break;
                case"7":
                    return;
                default:
                    System.out.println("指令错误");
                    break;

            }
        }
    }


    //访问页面
    public static void visitPage(Page page, Date time) {
        long num = page.getVisitNumber() + 1;
        page.setVisitNumber(num);
        long timeNow = time.getTime();
        page.setVisitTime(timeNow);

    }


    //网站访问量排行榜
    public static void showVisitRank(ArrayList<Page> list) {
        System.out.println("网站访问量排行榜:");
        Collections.sort(list);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getUrl());
        }
    }

    //所有网站总访问量
    public static void totalVisit(ArrayList<Page> list) {
        long count = 0;
        for (int i = 0; i < list.size(); i++) {
            count += list.get(i).getVisitNumber();
        }
        System.out.println("总访问量为:" + count);
    }

    //平均访问量
    public static void averageVisit(ArrayList<Page> list) {
        long count = 0;
        for (int i = 0; i < list.size(); i++) {
            count += list.get(i).getVisitNumber();
        }
        System.out.println("平均访问量为:" + count / list.size());

    }

    //显示页面最后访问时间,输入页面url，系统返回页面的最后访问时间
    public static void lastVisitTime(ArrayList<Page> list) {
        System.out.println("请输入要显示页面最后访问时间的url");
        Scanner scanner = new Scanner(System.in);
        String url = scanner.nextLine();
        int index = 0;

        for (int i = 0; i < 3; i++) {
            if(url.equals(list.get(i).getUrl())) {
                Date time = new Date(list.get(i).getVisitTime());
                System.out.println("该页面的最后访问时间为" + time);
                index = 1;
            }
        }
        if(index == 0) {
            System.out.println("该页面不存在");
        }

    }

    //清空访问统计
    public static void clearStatistics(ArrayList<Page> list) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setVisitTime(0);
            list.get(i).setVisitNumber(0);
        }
    }


}
